package com.qf.controller;

import com.netflix.discovery.EurekaClient;
import com.qf.feign.SearchFeign;
import com.qf.pojo.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * 客户管理
 * @author lixu
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private RestTemplate restTemplate;//远程调用的Http请求工具

    @Autowired
    private SearchFeign searchFeign;

    @Autowired
    private LoadBalancerClient loadBalancerClient;//Ribbin  spring-cloud-starter-ribbin

    //注册Eureka 注册表中获取 有点Low
  /*  @Autowired
    private EurekaClient eurekaClient;*/

    /**
     * 远程调用   小入门级程序
     */
    @GetMapping("/list")
    public String list(){

        //   uri:http://172.16.35.1:9012
        //1：去注册中心    QF-SEARCH找一堆地址  Ribbin
        ServiceInstance serviceInstance = loadBalancerClient.choose("QF-SEARCH");
        System.out.println("uri:" + serviceInstance.getUri());
        //远程调用搜索微服务
        //URL： 请求的搜索微服务的路径
        //Class<T>  ：返回值类型的Class
        String url = serviceInstance.getUri()   +"/search/result";
        //调用搜索微服务 ;
        String result = restTemplate.getForObject(url, String.class);
        return "我是客户微服务：开始远程调用:" + result ;
    }

    /**
     * 远程调用  Feign  SpringCloud技术之一
     */
    @GetMapping("/feign")
    public String feign(){
        String result = searchFeign.result();
        return "我是客户微服务feign：开始远程调用:" + result ;
    }
    /**
     * 远程调用  Feign  SpringCloud技术之一
     *
     *
     *   浏览器--》客户微服务  -->                        SearchFeign -->           搜索微服务
     *           Controller
     *           SpringMvc
     * /param1?name=xixi&age=21              请求的后面的路径：/param1/{name}/{age}
     *    param1(String name,Integer age){     searchFeign.param1(name,age);
     */
    @GetMapping("/param1")
    public String param1(String name,Integer age){
        String result = searchFeign.param1(name, age);
        return "我是客户微服务，返回值是:" + result;
    }
    /**
     * 传递参数  对象
     */
    @PostMapping("/param2")
    public Customer param2(@RequestBody Customer customer){
        Customer customer1 = searchFeign.param2(customer);
        return customer1;
    }

}
